**************************************************************************************
*                                                                                    *
*    KAZAKH KEYBOARD LAYOUT                                                          *
*                                                                                    *
*    by mergen3107                                             *
*    December 16, 2018                                                               *
*    Tested only on Paperwhite 3 (5.9.7) so far.                                     *
*                                                                                    *
*    This patch replaces the original keyboard layouts' image.                       *
*    So it should be stable as long as you don't screw up syntaxis of keymaps,       *
*    or Amazon decides to change keyboard handling in future updates.                *
*                                                                                    *
*    BIG THANKS to eureka and kaznelson from the-ebook.org for their implicit help!  *
*                                                                                    *
*                                                                                    *
**************************************************************************************

DISCLAIMER:
PROCEED WITH CAUTION, COMMON SENSE, AND CONSISTENCY.
I HAVE NO RESPONSIBILITY IF YOU BRICK YOUR KINDLE.
JAILBREAK REQUIRED.

##### PREPARATION OF LAYOUT (Based on Russian one)

What I did:

1) In keymaps file, added "holds" above necessary letters which you want to have diacritics:
"hold": n,
where n is assigned diacritical number according to table below

2) Created sections for diacritical choices. See corresponding files.

3) Included (2) into the original keymap file's very end, right after last diacritic section,
but before keypad (just to be safe).

4) Added 'nearly equal' sign to diacritic  206 (=), keeping in mind coordinates change to existing diacritic keys.

5) Added symmetrical key for comma, to the left from spacebar.
The key takes x coordinate of spacebar, it is of the same width as that of period key.
The spacebar has new coordinate x equal to plus 1 comma key width of comma, and new width
equal to minus 1 comma key width in corresponding orientation.

6) Repeated (1-5) for landscape. Keep track of commas and curly bracket. Structure is crucially important!

7) Made two separate .gz (gzip) archives of these two keymap files via 7z:
ru-1072x1448.keymap.gz
ru-1448x1072.keymap.gz

#### INSTALLATION TO KINDLE
(BIG thanks for start-off thoughts to eureka from http://www.the-ebook.org/forum/viewtopic.php?p=1026526#1026526 )
It looks complicated, but the logic is simple: compile new keyboard image (a 'driver' so to say), and replace the original one.
This image file is used by Kindle at the very boot.

1) Put keymaps folder with 2 .gz archives to root directory (/mnt/us/)
The following actions require usbnet, or ssh, or Kindle terminal:
Line with $ at the beginning are command lines, followed by output (if it is important)

2) Make a backup of keyboard image file
$ mntroot rw
$ cp /usr/share/keyboard.sqsh /usr/share/keyboard.sqsh.bak
$ mntroot ro

3) Copy all original keyboard files to modify them, e.g.
$ cp -r /usr/share/keyboard /var/local/kb

4) Replace layouts' files with new layouts
$ mntroot rw
$ cp /mnt/us/keymaps/ru-1072x1448.keymap.gz /var/local/kb/ru/
$ cp /mnt/us/keymaps/ru-1448x1072.keymap.gz /var/local/kb/ru/
$ mntroot ro

5) Create a new image (or a 'driver'). See 'mksquashfs output' file in the archive for a good reference.
$ mksquashfs /var/local/kb /var/local/keyboard.sqsh
{should give you no errors. If it complains about availabe space, restart the framework}

6) Check the image by test-mounting it:
$ mkdir /tmp/kb
$ mount -o loop /var/local/keyboard.sqsh /tmp/kb
$ ls /tmp/kb
ar da de en_GB en_US es es_419 fr fr_CA gu hi
images it ja ko ml mr nl no pt_BR ru sv ta zh_CN
zh_TW
$ umount /tmp/kb && rmdir /tmp/kb	

7) Delete the folder
$ rm -rf /var/local/kb		

##### ONCE THE IMAGE FILE READY
(you can start over from here if you have a new keyboard image :D )

8) Find processes that currently use keyboard folder:
$ lsof +D/usr/share/keyboard +c0
{gives only list of kb}

9) Stop keyboard process
$ stop kb
kb stop/waiting

10) Find loop device used for mounting that part of filesystem:
$ mount | grep ' on /usr/share/keyboard ' | awk '{print $1}'
dev/loop/6

11) Unmount this part of FS and make the device found above free
(path in 2nd command found by 'mount' command. See accroding output file):
$ umount /usr/share/keyboard
$ umount /chroot/usr/share/keyboard
$ losetup -d /dev/loop/6

12) Move new image to its place
$ mntroot rw
$ mv /var/local/keyboard.sqsh /usr/share
$ mntroot ro

13) Mount new keyboard image file
$ mount -o loop=/dev/loop/6,norelatime,nodiratime,noatime -t squashfs /usr/share/keyboard.sqsh /usr/share/keyboard

14) Start keyboard
$ start kb	

15) If you added languages and want them to appear in the menu selection, restart the framework or restart Kindle
$ restart framework


#### APPENDICES

#### TABLE for diacritics' numbers
Although chosen arbitrarily, the numbers shouldn't repeat any existing diacritics' numbers.
(lowercase, uppercase, original letter/key to be held down, diacritics to be added):
401, 501 а ә
402, 502 ы і
403, 503 н ң
404, 504 г ғ
405, 505 у ү ұ
406, 506 о ө
407, 507 к қ
408, 508 х һ

410 + ±				(plus/minus sign)
411 $ ₸  			(Tenge sign)
412 - – — 			(hyphen, en dash, em dash)
(add to) 206 = ≈ 	(nearly equal sign)

#### SIZES (max screen resolutions is 1072x1448)

Width: 			100 (portrait)
				137 (landscape)
				
y-coordinates: 	-71 (portrait)
				-69 (landscape)
				
x-coordinates:
2 diacritics:	436, 536 (portrait)
				588, 725 (landscape)
				
3 diacritics:	385, 485, 585 (portrait)
				519, 656, 793 (landscape)

These keys are already centered.				
Thus, if you need more diacritics, start from these two cases (even or odd number),
and add/subtract 1 width from both sides symmetrically for new pair of diacritics.

I hope you enjoy your new keyboard!		
####		